package com.xxl.job.executor.repository;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.common.RedisKeyConstant;
import com.xxl.job.executor.model.*;
import com.xxl.job.executor.utils.JSONUtils;
import com.xxl.job.executor.utils.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询信号
 */
@Repository
public class SignalDefRepository {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询环控信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalDefHkModel queryHkSignal(String gatherId, String gatherResId, String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_HK_SIGNAL);

        String value=valueOperations.get("HK_"+gatherId+"_"+gatherResId+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                SignalDefHkModel signalDefHkModel = JSONUtils.json2pojo(value, SignalDefHkModel.class);
                return signalDefHkModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
     //   XxlJobLogger.log("没有{}对应的信号配置！", "HK_"+gatherId+"_"+gatherResId+"_"+pointAddr);
        return null;
    }



    /**
     * 查询环控信号 多个
     * @param gatherId
     * @return
     */
    public List<SignalDefHkModel> queryHkSignalList(String gatherId, String gatherResId, String pointAddr){

        List<SignalDefHkModel> signalDefHkModellList = new ArrayList<>();
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_HK_SIGNAL);
        List<String> signalDefHkModelJsonList = MapUtil.getLikeByMap(valueOperations.entries(),"HK_"+gatherId+"_"+gatherResId+"_"+pointAddr);
        if (signalDefHkModelJsonList != null && signalDefHkModelJsonList.size() > 0){
            for (String signalDefHkModelStr : signalDefHkModelJsonList) {
                if (StringUtils.isNotBlank(signalDefHkModelStr)) {
                    try {
                        SignalDefHkModel gdm = JSONUtils.json2pojo(signalDefHkModelStr, SignalDefHkModel.class);
                        if(gatherId.equals(gdm.getGatherId())){
                            signalDefHkModellList.add(gdm);
                        }
                    } catch (IOException e) {
                        XxlJobLogger.log("获取采集口配置信息终端JSON='{}' 转换异常！", signalDefHkModelStr, e);
                    }
                }
            }
            return signalDefHkModellList;
        }else {
            //XxlJobLogger.log("没有'{}' 对应的信号配置缓存！", "HK_"+gatherId+"_"+gatherResId+"_"+pointAddr);
            return null;
        }
    }
    /**
     * 查询入侵信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public List<SignalDefRqModel> queryRqSignalList(String gatherId, String gatherResId,String signalType, String pointAddr) {

        List<SignalDefRqModel> signalDefRqModellList = new ArrayList<>();
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_RQ_SIGNAL);
        List<String> signalDefRqModelJsonList = MapUtil.getLikeByMap(valueOperations.entries(),"RQ_"+gatherId+"_"+gatherResId+"_"+signalType+"_");
        if (signalDefRqModelJsonList != null && signalDefRqModelJsonList.size() > 0){
            for (String signalDefRqModelStr : signalDefRqModelJsonList) {
                if (StringUtils.isNotBlank(signalDefRqModelStr)) {
                    try {
                        SignalDefRqModel gdm = JSONUtils.json2pojo(signalDefRqModelStr, SignalDefRqModel.class);
                        if(gatherId.equals(gdm.getGatherId())){
                            signalDefRqModellList.add(gdm);
                        }
                    } catch (IOException e) {
                        XxlJobLogger.log("获取采集口配置信息终端JSON='{}' 转换异常！", signalDefRqModelStr, e);
                    }
                }
            }
            return signalDefRqModellList;
        }else {
            XxlJobLogger.log("没有'{}' 对应的信号配置缓存！", "RQ_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr);
            return null;
        }
    }

    /**
     * 查询入侵信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalDefRqModel queryRqSignal(String gatherId, String gatherResId,String signalType, String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_RQ_SIGNAL);

        String value=valueOperations.get("RQ_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                SignalDefRqModel signalDefRqModel = JSONUtils.json2pojo(value, SignalDefRqModel.class);
                return signalDefRqModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
        XxlJobLogger.log("没有{}对应的信号配置！", "RQ_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr);
        return null;
    }

    /**
     * 查询电话信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalDefDhModel queryDhSignal(String gatherId, String gatherResId, String signalType,String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_DH_SIGNAL);

        String value=valueOperations.get("DH_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                SignalDefDhModel signalDefDhModel = JSONUtils.json2pojo(value, SignalDefDhModel.class);
                return signalDefDhModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
        XxlJobLogger.log("没有{}对应的信号配置！", "DH_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr);
        return null;
    }

    /**
     * 查询门禁信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalMjDhModel queryMjSignal(String gatherId, String gatherResId, String signalType,String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_MJ_SIGNAL);

        //获取redis中的
        String value=valueOperations.get("MJ_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                //json数据封装成实体对象
                SignalMjDhModel signalMjDhModel = JSONUtils.json2pojo(value, SignalMjDhModel.class);
                return signalMjDhModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
        XxlJobLogger.log("没有{}对应的信号配置！", "DH_"+gatherId+"_"+gatherResId+"_"+signalType+"_"+pointAddr);
        return null;
    }



    /**
     * 查询消防信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalDefXfModel queryXfSignal(String gatherId, String gatherResId,String registerAddr,String moduleAddr, String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_XF_SIGNAL);

        String value=valueOperations.get("XF_"+gatherId+"_"+gatherResId+"_"+registerAddr+
                "_"+moduleAddr+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                SignalDefXfModel signalDefXfModel = JSONUtils.json2pojo(value, SignalDefXfModel.class);
                return signalDefXfModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
        XxlJobLogger.log("没有{}对应的信号配置！", "XF_"+gatherId+"_"+gatherResId+"_"+registerAddr+
                "_"+moduleAddr+"_"+pointAddr);
        return null;
    }

    /**
     * 查询AP信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalDefApModel queryApSignal(String gatherId, String gatherResId, String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_AP_SIGNAL);

        String value=valueOperations.get("GATHER_"+gatherId+"_"+gatherResId+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                SignalDefApModel signalDefApModel = JSONUtils.json2pojo(value, SignalDefApModel.class);
                return signalDefApModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
        return null;
    }

    /**
     * 查询采集口信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalDefGatherModel queryGatherSignal(String gatherId, String gatherResId, String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_GATHER_SIGNAL);

        String value=valueOperations.get("GATHER_"+gatherId+"_"+gatherResId+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                SignalDefGatherModel signalDefGatherModel = JSONUtils.json2pojo(value, SignalDefGatherModel.class);
                return signalDefGatherModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
        return null;
    }

}
